23.2 Performance Monitoring Initialization
The routine perf_event_init() bootstraps the Performance Monitoring (Perf) subsystem, assuming responsibility for building the essential infrastructure required to manage performance events during the early stages of system initialization. It prepares the foundational data structures utilized to audit both CPU hardware events (such as Cache Misses) and internal software execution anomalies. Because performance monitoring relies heavily on high-precision temporal sampling, this subsystem must execute its initialization paths sequentially following the conclusion of time_init().
The function is hosted within the source core tree file git/kernel/events/core.c, and its definition is implemented as follows:
void __init perf_event_init(void)
{
int ret;
idr_init(&pmu_idr);
perf_event_init_all_cpus();
init_srcu_struct(&pmus_srcu);
perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
perf_pmu_register(&perf_cpu_clock, NULL, -1);
perf_pmu_register(&perf_task_clock, NULL, -1);
perf_tp_register();
perf_event_init_cpu(smp_processor_id());
register_reboot_notifier(&perf_reboot_notifier);
ret = init_hw_breakpoint();
WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) != 1024);
}
The principal technical task of perf_event_init() is to bootstrap the global data structures utilized by the kernel to manage Performance Monitoring Counters (PMCs) and software-defined tracing events. This implementation demonstrates exactly how the performance monitoring subsystem builds its administrative tracking framework from the ground up.
First, the routine establishes its core tracking infrastructure by invoking the functions idr_init(), perf_event_init_all_cpus(), and init_srcu_struct(). This block initializes the system's integer ID allocator, the per-CPU private data structures, and a dedicated Sleepable Read-Copy-Update (SRCU) structure. The integer ID allocator is leveraged by the Linux kernel to assign a unique identification index to every registered Performance Monitoring Unit (PMU), enabling user-space applications to resolve the matching hardware driver via its ID. The per-CPU private data structures support high-speed parallel event sampling across symmetric multiprocessing environments. Concurrently, the sleepable RCU synchronization framework guarantees that while the kernel frequently reads the registered PMU list, any concurrent loading or unloading of dynamic PMU drivers will not induce a system crash or a terminal deadlock condition.
Following this infrastructure setup, the function dispatches perf_pmu_register() to register three software-driven performance probes: perf_swevent, perf_cpu_clock, and perf_task_clock. These probes are implemented entirely in software and operate without requiring specialized architectural CPU hardware counters. perf_swevent orchestrates the auditing of software events inside the kernel (such as tracking page faults or context switches), while perf_cpu_clock and perf_task_clock leverage the kernel's high-resolution timers to supply precise time-based sampling profiles.
Upon completing the registration of these software probes, the function dispatches perf_tp_register() to link the performance monitoring subsystem directly with the kernel's event tracing core. This linkage empowers users to audit low-level kernel workflows by issuing standard user-space diagnostic commands like perf record -e sched:sched_switch.
Next, the initialization routine invokes init_hw_breakpoint() to bootstrap the platform's hardware breakpoint architecture. This subsystem is predominantly utilized during low-level debugging sessions to monitor exactly when a specified memory address undergoes a read or write transaction (matching the microarchitectural behavior of a hardware watchpoint within tools like gdb).
Following this pass, the function executes perf_event_init_cpu() to formally activate the performance monitoring framework on the active primary bootstrap CPU core.
The final assertion macro BUILD_BUG_ON() executes a static validation pass during the compilation phase. It verifies that the exact memory offset of the data_head field enclosed within struct perf_event_mmap_page evaluates strictly to 1024 bytes. This compilation boundary is mandatory to guarantee that when the kernel shares memory with user-space diagnostic applications (such as the perf tool) via an mmap() interface, the structural memory layout of the communication canvas remains identical across the boundary; a misalignment here would cause the parsed performance telemetry payload to decode as complete garbage.